package com.als.healthymananger.conf;

import com.als.healthymananger.dto.AppointmentDto;
import com.als.healthymananger.dto.JurisdictionDto;
import com.als.healthymananger.entity.*;
import com.als.healthymananger.handler.AuthorizationHandler;
import com.als.healthymananger.handler.JurisdictionHandler;
import com.als.healthymananger.interceptor.GlobalInterceptor;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Autowired
    private GlobalInterceptor globalInterceptor;

    @Bean
    public OpenAPI crm() {
        return new OpenAPI()
                .info(new Info()
                        .title("healthyMananger")
                        .version("1.0")
                        .license(new License()
                                .name("apache 2.0")
                                .url("http:springdoc.org")))
                .components(new Components()
                        // 添加全局安全验证
                        // 在需要安全验证的api中添加@SecurityRequirement标签
                        // 如： @Operation(security = { @SecurityRequirement(name = "bearer-key") })
                        .addSecuritySchemes("bearer-key", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT"))

                        .addSchemas("simpleAppointment", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Appointment.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("report", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Report.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("sport", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Sport.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("diet", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Diet.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("suggestion", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Suggestion.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("assessment", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Assessment.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("intervene", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Intervene.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("interveneTemplate", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(InterveneTemplate.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("archive", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(Archive.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("user", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(User.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("userType", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(UserType.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("jurisdiction", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(JurisdictionDto.class).resolveAsRef(false))
                                .schema)
                        .addSchemas("appointment", ModelConverters.getInstance()
                                .resolveAsResolvedSchema(new AnnotatedType(AppointmentDto.class).resolveAsRef(false))
                                .schema));
    }

    // 配置过滤器
    @Bean
    public FilterRegistrationBean authorizationHandlerRegistration() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new AuthorizationHandler());
        registrationBean.setName("AuthorizationHandler");
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter("excludeUrls", "/login/sign-in,/login/sign-up,/login/foget,/util/unauthorized,/util/forbidden,/doc.html");
        registrationBean.setOrder(2);
        return  registrationBean;
    }

    // 配置过滤器
//    @Bean
    public FilterRegistrationBean jurisdictionHandlerRegistration() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new JurisdictionHandler());
        registrationBean.setName("jurisdictionHandler");
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter("excludeUrls", "/login/sign-in,/login/sign-up,/login/foget,/util/unauthorized,/util/forbidden");
        registrationBean.setOrder(1);
        return  registrationBean;
    }

    // 配置拦截器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(globalInterceptor)
                // 需要拦截的路径
                .addPathPatterns("/**")
                // 放行不需要拦截的路径
                .excludePathPatterns("/swagger-ui.html", "/swagger-ui/*", "/api-doc", "/api-doc/*", "/login/*", "/util/*", "/doc.html");
    }
}
